package ds.stack;

import ds.linklist.simple.Link;
import ds.linklist.simple.LinkList;

public class LinkStack {

	private LinkList theList;
	//------------------------------------------
	public LinkStack(){
		theList = new LinkList();
	}
	
	public void push(int i)
	{
		theList.insertFirst(i);
	}
	
	public int pop() throws Exception
	{
		Link popLink = theList.deleteFirst();
		if (popLink!=null)
			return theList.deleteFirst().getData();
		else
			throw new Exception("栈已为空，无法pop");
	}
	
	public boolean isEmpty()
	{
		return theList.isEmpty();
	}
	
	public int peek() throws Exception
	{
		Link firstLink = theList.getFirst();
		if(firstLink != null)
			return firstLink.getData();
		else
			throw new Exception("栈为空，无法peek");
	} 
	
	public void displayStack()
	{
		theList.displayList();
	}
}

class LinkStackApp {
	public static void main(String[] args) throws Exception
	{
		LinkStack ls = new LinkStack();
		int i=0;
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		ls.push(i++);
		
		ls.displayStack();
		
		ls.pop();
		ls.pop();
		ls.pop();

		ls.displayStack();
		System.out.print(ls.peek());
	}
}